/*
 * author      : 吴中勤 
 * date        : 2018/9/14
 * description : TODO
 */

package com.deepexi.adapter.converter.sap;

import com.deepexi.adapter.common.exception.HttpNon200Exception;
import com.deepexi.adapter.common.sdk.RequestInfo;
import com.deepexi.adapter.sdk.sap.SapSdk;
import com.deepexi.adapter.sdk.sap.dto.SapBaseResponse;
import com.deepexi.adapter.sdk.sap.dto.fund.SapFundResponseBody;
import com.deepexi.adapter.sdk.sap.exception.SapBizException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class SapResponseHandler {

    @Autowired
    private SapSdkIntercepter sapSdkIntercepter;

    @Autowired
    private SapSdk sapSdk;

    public SapFundResponseBody requestAndThrowIfError(RequestInfo requestInfo)
            throws IOException, HttpNon200Exception, SapBizException {

        String respBodyStr = sapSdkIntercepter.doHttpAndLogWithRetryIfFail(requestInfo);    // 日志和重试
        SapFundResponseBody responseBody = sapSdk.parseResult(requestInfo, respBodyStr);

        // 处理结果
        if (null == responseBody)
            throw new SapBizException("sap支付出错, 返回结果: null");
        if (!responseBody.isOk())
            throw new SapBizException("sap支付出错, 返回结果:" + responseBody.toString());

        return responseBody;
    }
}
